package com.lagou.controller;


import com.lagou.domain.Course;
import com.lagou.domain.CourseLesson;
import com.lagou.domain.CourseSection;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    /*根据课程ID查询对应的课程内容（章节+课时）*/
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseId(Integer courseId) {

        List<CourseSection> sectionAndLessonByCourseId = courseContentService.findSectionAndLessonByCourseId(courseId);

        ResponseResult responseResult = new ResponseResult(true, 200, "章节及课时内容查询成功", sectionAndLessonByCourseId);
        return responseResult;
    }

    /*回显章节对应的课程信息*/
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId) {
        Course courseByCourseId = courseContentService.findCourseByCourseId(courseId);
        ResponseResult responseResult = new ResponseResult(true, 200, "查询课程名称成功", courseByCourseId);
        return responseResult;
    }


    /*保存和修改章节信息*/
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection) {
        ResponseResult responseResult = null;
        if (courseSection.getId() == null) {
            courseContentService.saveSection(courseSection);
            responseResult = new ResponseResult(true, 200, "新增章节成功", null);

        } else {
            courseContentService.updateSection(courseSection);
            responseResult = new ResponseResult(true, 200, "修改章节成功", null);
        }
        return responseResult;
    }

    /**
     * 修改章节状态
     * 状态，0:隐藏；1：待更新；2：已发布
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(@RequestParam int id, @RequestParam int status) {
        try {
            courseContentService.updateSectionStatus(id, status);
            //封装最新的状态信息
            Map<String, Integer> map = new HashMap<>();
            map.put("status", status);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存&修改课时
     */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson lesson) {
        try {
            if (lesson.getId() == null) {
                courseContentService.saveLesson(lesson);
                return new ResponseResult(true, 200, "响应成功", null);
            } else {
                courseContentService.updateLesson(lesson);
                return new ResponseResult(true, 200, "响应成功", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
